package com.luomo.java.juc.producerconsumer;

/**
 * @author LiuMei
 * @date 2021-04-12.
 */
public class SyncShareData extends ShareData{
    @Override
    public synchronized void add() {
        while (number != 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"\t生产");
        number++;
        this.notifyAll();
    }

    @Override
    public synchronized void sub() {
        while (number == 0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"\t消费");
        number--;
        this.notifyAll();
    }
}
